<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\UpdateExpress;
use App\Admin\Forms\UpdateLogistics;
use App\Admin\Repositories\GrOrder;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class GrOrderController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new GrOrder(['userInfo']), function (Grid $grid) {
            $grid->export();
            $grid->setActionClass(Grid\Displayers\Actions::class);
            $grid->disableEditButton();
            $grid->column('id')->sortable();
            $grid->column('order_sn');
            $grid->column('userInfo.real_name','买家');
            $grid->column('total_money');
            $grid->column('delivery_type','配送方式')->display(function ($info){
                switch ($info){
                    case 1:return '自提';
                    case 2:return '快递';
                }
            })->explode()->label('default');
            $grid->column('order_status','订单状态')->using([-2 => '未支付',-1 => '已取消',0 => '未发货', 1 => '配送中', 2 => '已收货', 3 => '超时未支付'])->label([
                -2 => 'default',
                -1 => 'danger',
                0 => 'default',
                1 => 'primary',
                2 => 'success',
                3 => 'danger',
                4 => Admin::color()->info(),
            ])->filter(
                Grid\Column\Filter\In::make([
                    -2 => '未支付',-1 => '已取消',0 => '未发货', 1 => '配送中', 2 => '已收货', 3 => '超时未支付'
                ])
            );

            $grid->column('','发货')->display('发货')->modal(function (Grid\Displayers\Modal $modal) {
                if ($this->delivery_status==1){
                    // 标题
                    $modal->title('弹窗标题');
                    // 自定义图标
                    $modal->icon('feather icon-edit');
                    // 传递当前行字段值
                    return UpdateLogistics::make()->payload(['id' => $this->id]);
                }else{
                    $modal->title('该商品已发货');
                    $modal->icon('feather icon-check');
                }

            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new GrOrder(), function (Show $show) {
            $show->field('id');
            $show->field('order_sn');
            $show->field('user_id');
            $show->field('order_status');
            $show->field('goods_money');
            $show->field('total_money');
            $show->field('is_pay');
            $show->field('is_appraise');
            $show->field('is_closed');
            $show->field('address_id');
            $show->field('express_id');
            $show->field('express_company');
            $show->field('express_no');
            $show->field('delivery_status');
            $show->field('delivery_time');
            $show->field('receipt_status');
            $show->field('pay_time');
            $show->field('is_comment');
            $show->field('pay_status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new GrOrder(), function (Form $form) {
            $form->display('id');
            $form->text('order_sn');
            $form->text('user_id');
            $form->text('order_status');
            $form->text('goods_money');
            $form->text('total_money');
            $form->text('is_pay');
            $form->text('is_appraise');
            $form->text('is_closed');
            $form->text('address_id');
            $form->text('express_id');
            $form->text('express_company');
            $form->text('express_no');
            $form->text('delivery_status');
            $form->text('delivery_time');
            $form->text('receipt_status');
            $form->text('pay_time');
            $form->text('is_comment');
            $form->text('pay_status');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
